package it.polimi.swKnights.SWIMv2.servlets.administration;

import it.polimi.swKnights.SWIMv2.ejb.administration.NominateAdminRemote;
import it.polimi.swKnights.SWIMv2.ejb.exceptions.InvalidLoginPrivilegesException;
import it.polimi.swKnights.SWIMv2.ejb.exceptions.LoginFailException;
import it.polimi.swKnights.SWIMv2.ejb.exceptions.NoSelfRemoveException;
import it.polimi.swKnights.SWIMv2.general.Errors;
import it.polimi.swKnights.SWIMv2.servlets.SwimServlet;

import java.io.IOException;

import javax.ejb.EJBException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Servlet implementation class DeleteAdminProcessData
 */
public class DeleteAdminProcessData extends SwimServlet
{
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException
	{
		HttpSession session = request.getSession();
		String founder = (String) session.getAttribute("userName");
		String password = (String) session.getAttribute("password");
		String oldAdmin = request.getParameter("oldAdmin");

		NominateAdminRemote adminBean = this.lookUp(NominateAdminRemote.class);

		String parameters = "";

		try
		{

			adminBean.removeFromAdmins(oldAdmin, founder, password);
			parameters = "adminRemoved=" + oldAdmin;

		}
		catch (LoginFailException e)
		{
			parameters = "error=" + Errors.session;
		}
		catch (EJBException e)
		{
			parameters = "error=" + Errors.noexistuser;
		}
		catch (InvalidLoginPrivilegesException e)
		{
			parameters = "error=" + Errors.notfounder;
		}
		catch (NoSelfRemoveException e)
		{
			parameters = "error=" + Errors.noselfremove;
		}

		response.sendRedirect("./administration?" + parameters);

	}

}
